package org.example.service;

import jakarta.annotation.Resource;
import org.example.entity.MyUserDetails;
import org.example.entity.ResponseModel;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

@Service
public class LogoutService {


    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @GetMapping("/logout")
    public ResponseModel<String> logout(){
        //删除redis中的token信息
        String redisKey = null;
        MyUserDetails userDetails = (MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        if(principal!=null){
//
//        }
        redisKey = "user:"+userDetails.getMyUser().getId();
        redisTemplate.delete(redisKey);
        //清空安全上下文
        SecurityContextHolder.clearContext();
        return ResponseModel.logoutSuccess("退出成功");
    }
}
